/****************************************************************
 *
 *
 ****************************************************************/

/* Standard includes. */
#include <stdio.h>

/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "stm32f10x_conf.h"
#include "modbus.h"


/* Task priorities. Higher number is higher priority */
#define TASK_UI_PRIORITY        ( tskIDLE_PRIORITY + 1 )
#define TASK_COMM_PRIORITY      ( tskIDLE_PRIORITY + 4 )  // highest
#define TASK_MAIN_PRIORITY      ( tskIDLE_PRIORITY + 3 )


/* Tasks */
static void prvSetupHardware( void );

static void vTaskUI( void *pvParameters );
static void vTaskComm( void *pvParameters );
static void vTaskMain( void *pvParameters );

static xQueueHandle xQueueUI;
static xQueueHandle xQueueMain;

/* Main Program*/
int main( void )
{
    /*!< At this stage the microcontroller clock setting is already configured, 
     this is done through SystemInit() function which is called from startup
     file (startup_stm32f10x_xx.s) before to branch to application main.
     To reconfigure the default setting of SystemInit() function, refer to
     system_stm32f10x.c file
    */ 
    
    /* Hardware Initialization */
    prvSetupHardware();
    
    /* Creat task: MODBUS*/
    xTaskCreate(	vTaskComm,"MODBUS", configMINIMAL_STACK_SIZE+500, NULL, TASK_COMM_PRIORITY, NULL );  
    
    /* Creat Task: UI */
    xTaskCreate(	vTaskUI,"UI",configMINIMAL_STACK_SIZE+500,NULL,TASK_UI_PRIORITY,NULL );  
    
    /* Creat Task: Main */
    xTaskCreate(	vTaskMain,"UI",configMINIMAL_STACK_SIZE+500,NULL,TASK_MAIN_PRIORITY,NULL );  
    
    /* Start the scheduler. */
    vTaskStartScheduler();
    
    /* Will only get here if there was not enough heap space to create the
    idle task. */
    return 0;
}
    

static void prvSetupHardware( void )
{ 
    // Init Systick Timer for RTOS Scheduler
    while(SysTick_Config(SystemCoreClock / 1000));
}


static void vTaskUI( void *pvParameters )
{
    unsigned char recVal;
    
    xQueueUI = xQueueCreate(5, sizeof(unsigned char));
 
    /* As per most tasks, this task is implemented in an infinite loop. */
    for( ;; )
    {
        xQueueReceive( xQueueUI,&recVal, portMAX_DELAY );  
        switch (recVal)
        {
 
        }
    }  
}
 


static void vTaskComm( void *pvParameters )
{
    static unsigned long count;

    // Use MB_EventSent(MB_Event e) to send event to MODBUS
    if( MB_Open(10) == MB_ERR_NONE)
    {
        MB_State =  MB_STATE_ENABLED;
        //MB_EventSend( MB_EV_FRAME_RECEIVED);  // simulation
    }
    
    /* As per most tasks, this task is implemented in an infinite loop. */
    for( ;; )
    {
        while(1)
        {
            MB_ProcessingLoop();
            count++;
        }
        // Delay100ms
        //vTaskDelay( 100 / portTICK_RATE_MS );   
    }  
}



static void vTaskMain( void *pvParameters )
{
    xQueueMain = xQueueCreate(5, sizeof(unsigned char));
    
    /* As per most tasks, this task is implemented in an infinite loop. */
    for( ;; )
    {
 
    }  
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}

#endif

/**
  * @}
  */

/**
  * @}
  */

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
